var plot;

function runPlot(series, tickInterval, seriesNames) {
    $.jqplot.config.enablePlugins = true;
    $.jqplot.config.fontSize = 12;
	
	var options = {
		title : 'Producción energética en MWh: zona geográfica / tipo de energía',
		axes: {
			xaxis: {
                renderer:$.jqplot.DateAxisRenderer, 
                tickInterval:tickInterval,
                rendererOptions:{
                    tickRenderer:$.jqplot.CanvasAxisTickRenderer
                },
                tickOptions:{
                	formatString:'%m/%Y', 
                	fontSize:'10pt', 
                	angle:-40 
                }
			},
		    yaxis: {
		    	min: 0,
				autoscale: true,
			    labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
				tickOptions: {
					formatString: '%i',
					fontSize: 12
				}
		    }
		},
		highlighter: {
			sizeAdjust: 10,
			tooltipLocation: 'n',
			tooltipAxes: 'y',
			tooltipFormatString: '<strong><span>%s</span></strong>',
			useAxesFormatters: false
		},
		cursor: {
			style: 'pointer',
			show: true
		},
		legend:{
			show:true, 
			labels:seriesNames,
			rendererOptions:{
				numberColumns:2
			}
		} 		
	};
	
	plot = $.jqplot('chartdiv', series, options);
}

function removeSerie(uuid) {
	var timelineUrl = $('#timelineUrl').val();
	$('#' + uuid).remove();
	
	jQuery.ajax(
			{type:'POST',
				data:'add=false&uuid=' + uuid, 
				url:timelineUrl,
				success:function(data,textStatus){jQuery('#graph').html(data);}
			}
		);
	return false;
}

function addSerie(serieId, region, province, energy) {
	var regionName = 'España';
	
	if(region != null && region != '') {
		regionName = region;
	} else if(province != null && province != '') {
		regionName = province;
	}
	
	var serie = '<li class="serieItem" id="' + serieId + '"><span>' + regionName + ' + ' + energy + '</span> ';
	serie += '<a href="#" onclick="return removeSerie(\'' + serieId + '\');">eliminar</a>';
	serie += '</li>';
	
	$('#list ul').append(serie);
}

function onSubmitAddButton() {
	var serieId = uuid();
	var timelineUrl = $('#timelineUrl').val();
	var region, province, energy;
	var regionName, provinceName, energyName;
	var allRegions = false;
	var allEnergies = false;
	var myData;
	
	energy = $('#energy').val();
	region = $('#region').val();
	
	if(energy == -1) {
		allEnergies = true;
		energyName = 'Todas las energías';
	} else {
		energyName = $('#energy option:selected').html();
	}

	if(region != null && region.match("^P_")) {
		// It's a province
		province = region.replace(/^P_/, "");
		provinceName = $('#region option:selected').html();
		regionName = '';
		region = '';
	} else if(region != null && region != -1) {
		// It's a region
		region = region.replace(/^R_/, "");
		regionName = $('#region option:selected').html();
		provinceName = '';
		province = '';
	} else if(region == -1) {
		allRegions = true;
	}

	if(allRegions || region != null || province != null) {
		addSerie(serieId, regionName, provinceName, energyName);
		
		myData = 'add=true&uuid=' + serieId + '&allRegions=' + allRegions + '&allEnergies=' + allEnergies + '&region=' + region + '&province=' + province + '&energy=' + energy;
		myData += '&monthStart=' + $('#initMonth').val();
		myData += '&monthEnd=' + $('#endMonth').val();
		myData += '&yearStart=' + $('#initYear').val();
		myData += '&yearEnd=' + $('#endYear').val();
			
		jQuery.ajax(
				{type:'POST',
					data:myData, 
					url:timelineUrl,
					success:function(data,textStatus){jQuery('#graph').html(data);}
				}
			);
	}
	
	return false;
}